package com.ruoyi.project.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.domain.OrderPreOrder;
import com.ruoyi.project.mapper.OrderPreOrderMapper;
import com.ruoyi.project.service.IOrderPreOrderService;

/**
 * 可派单Service业务层处理
 * 
 * @author phil
 * @date 2020-05-31
 */
@Service
public class OrderPreOrderServiceImpl implements IOrderPreOrderService{
	@Autowired
	private OrderPreOrderMapper orderPreOrderMapper;

	/**
	 * 查询可派单
	 * 
	 * @param id 可派单ID
	 * @return 可派单
	 */
	@Override
	public OrderPreOrder selectOrderPreOrderById(Long id){
		return orderPreOrderMapper.selectOrderPreOrderById(id);
	}

	/**
	 * 查询可派单列表
	 * 
	 * @param orderPreOrder 可派单
	 * @return 可派单
	 */
	@Override
	public List<OrderPreOrder> selectOrderPreOrderList(OrderPreOrder orderPreOrder){
		return orderPreOrderMapper.selectOrderPreOrderList(orderPreOrder);
	}

	/**
	 * 新增可派单
	 * 
	 * @param orderPreOrder 可派单
	 * @return 结果
	 */
	@Override
	public int insertOrderPreOrder(OrderPreOrder orderPreOrder){
		orderPreOrder.setCreateTime(DateUtils.getNowDate());
		return orderPreOrderMapper.insertOrderPreOrder(orderPreOrder);
	}

	/**
	 * 修改可派单
	 * 
	 * @param orderPreOrder 可派单
	 * @return 结果
	 */
	@Override
	public int updateOrderPreOrder(OrderPreOrder orderPreOrder){
		return orderPreOrderMapper.updateOrderPreOrder(orderPreOrder);
	}

	/**
	 * 删除可派单对象
	 * 
	 * @param ids 需要删除的数据ID
	 * @return 结果
	 */
	@Override
	public int deleteOrderPreOrderByIds(String ids){
		return orderPreOrderMapper.deleteOrderPreOrderByIds(Convert.toStrArray(ids));
	}

	/**
	 * 删除可派单信息
	 * 
	 * @param id 可派单ID
	 * @return 结果
	 */
	@Override
	public int deleteOrderPreOrderById(Long id){
		return orderPreOrderMapper.deleteOrderPreOrderById(id);
	}

	@Override
	public List<Map<String, Object>> getPreOrderList(Map<String, Object> paramMap){
		Map<String, Object> resultMap = new HashMap<>();
		// 查询内容转换
		List<Map<String, Object>> selectedList = orderPreOrderMapper.getPreOrderList(paramMap);
		if(selectedList != null && selectedList.size() > 0){
			for(Map<String, Object> tempMap : selectedList){
				String customer = StringUtils.defaultString((String)tempMap.get("customer"));
				// 包含这个key,直接把当前内容加入到列表
				if(resultMap.containsKey(customer)){
					((List)resultMap.get(customer)).add(tempMap);
				}else{// 不包含就创建一个新List放入
					List<Map> newList = new ArrayList<Map>(16);
					newList.add(tempMap);
					resultMap.put(customer, newList);
				}
			}
		}

		// 数据格式化,先遍历Map取值，转换成Map
		List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
		if(!resultMap.isEmpty()){
			for(String key : resultMap.keySet()){
				Map<String, Object> tempMap = new HashMap<>();
				tempMap.put("customer", key);
				tempMap.put("list", (List)resultMap.get(key));
				resultList.add(tempMap);
			}
		}

		// 格式转换
		return resultList;
	}
}
